<?php
class owFunciones
{	
	// Tranforma fecha es_AR en fecha MySQL 02/12/2009 a 2009-12-02
	public static function DateToSQL($fecha){
		$fecha = explode("/", $fecha);
		if (count($fecha) == 3){
			return ($fecha[2] . "-" . $fecha[1] . "-" . $fecha[0]);
		}
		else{
			return false;
		}
	}
	
	public static function ErroresOW($NumError)
	{
		SWITCH ($NumError)
		{
			CASE 1452:
				return "DNI Inexistente";
			break;
		} 
	}
	
	// Transforma un Numero a Texto: 1000 = Mil
	public static function NumberToString($value)
	{
		$integer = explode(".", $value);
		if (count($integer) == 2)
		{
			return self::_MilMillon($integer[0]) . ' PESOS CON ' . self::_MilMillon($integer[1]) . " CENTAVOS";
		}
		else
		{
			$integer = explode(",", $value);
			if (count($integer) == 2)
			{
				return self::_MilMillon($integer[0]) . ' PESOS CON ' . self::_MilMillon($integer[1]) . " CENTAVOS";
			}
			else
			{
				return self::_MilMillon($integer[0]) . ' PESOS';
			}
		}
	}
	private function _Unidad($number)
	{
		switch ($number)
		{
			case 9: return "NUEVE"; break;
			case 8:	return "OCHO";	break;
			case 7: return "SIETE"; break;
			case 6:	return "SEIS";	break;
			case 5:	return "CINCO"; break;
			case 4:	return "CUATRO"; break;
			case 3: return "TRES"; break;
			case 2: return "DOS"; break;
			case 1: return "UN"; break;
			case 0: return "";	break;
		}
	}
	private function _Decena($number)
	{
		if ($number >= 90 && $number <= 99) {
			$num = "NOVENTA "; 
			if ($number > 90) $num = $num."Y ".(self::_Unidad($number - 90));
		} else if ($number >= 80 && $number <= 89) {
			$num = "OCHENTA ";
			if ($number > 80) $num = $num."Y ".(self::_Unidad($number - 80));
		} else if ($number >= 70 && $number <= 79) {
			$num = "SETENTA ";
			if ($number > 70) $num = $num."Y ".(self::_Unidad($number - 70));
		} else if ($number >= 60 && $number <= 69) {
			$num = "SESENTA ";
			if ($number > 60) $num = $num."Y ".(self::_Unidad($number - 60));
		} else if ($number >= 50 && $number <= 59) {
			$num = "CINCUENTA ";
			if ($number > 50) $num = $num."Y ".(self::_Unidad($number - 50));
		} else if ($number >= 40 && $number <= 49) {
			$num = "CUARENTA ";
			if ($number > 40) $num = $num."Y ".(self::_Unidad($number - 40));
		} else if ($number >= 30 && $number <= 39) {
			$num = "TREINTA ";
			if ($number > 30) $num = $num."Y ".(self::_Unidad($number - 30));
		} else if ($number >= 20 && $number <= 29) {
			if ($number == 20) $num = "VEINTE ";
			else $num = "VEINTI".(self::_Unidad($number - 20));
		} else if ($number >= 10 && $number <= 19) {
			switch ($number)
			{
				case 10: $num = "DIEZ "; break;
				case 11: $num = "ONCE "; break;
				case 12: $num = "DOCE "; break;
				case 13: $num = "TRECE "; break;
				case 14: $num = "CATORCE "; break;
				case 15: $num = "QUINCE ";	break;
				case 16: $num = "DIECISEIS "; break;
				case 17: $num = "DIECISIETE "; break;
				case 18: $num = "DIECIOCHO "; break;
				case 19: $num = "DIECINUEVE "; break;
			}	
		} else $num = self::_Unidad($number);
		return $num;	
	}
	private function _Centena($number)
	{
		if ($number >= 100) {
			if ($number >= 900 && $number <= 999) {
				$num = "NOVECIENTOS ";
				if ($number > 900) $num = $num.(self::_Decena($number - 900));
			} else if ($number >= 800 && $number <= 899) {
				$num = "OCHOCIENTOS ";
				if ($number > 800) $num = $num.(self::_Decena($number - 800));
			} else if ($number >= 700 && $number <= 799) {
				$num = "SETECIENTOS ";
				if ($number > 700) $num = $num.(self::_Decena($number - 700));
			} else if ($number >= 600 && $number <= 699) {
				$num = "SEISCIENTOS ";
				if ($number > 600) $num = $num.(self::_Decena($number - 600));
			} else if ($number >= 500 && $number <= 599) {
				$num = "QUINIENTOS ";
				if ($number > 500) $num = $num.(self::_Decena($number - 500));
			} else if ($number >= 400 && $number <= 499) {
				$num = "CUATROCIENTOS ";
				if ($number > 400) $num = $num.(self::_Decena($number - 400));
			} else if ($number >= 300 && $number <= 399) {
				$num = "TRESCIENTOS ";
				if ($number > 300) $num = $num.(self::_Decena($number - 300));
			} else if ($number >= 200 && $number <= 299) {
				$num = "DOSCIENTOS ";
				if ($number > 200) $num = $num.(self::_Decena($number - 200));
			} else if ($number >= 100 && $number <= 199) {
				if ($number == 100)	$num = "CIEN ";
				else $num = "CIENTO ".(self::_Decena($number - 100));
			}
		}
		else $num = self::_Decena($number);
		return $num;
	}
	private function _Miles($number)
	{
		if ($number >= 1000 && $number < 2000) $num = "MIL ".(self::_Centena($number%1000));
		if ($number >= 2000 && $number <10000) $num = self::_Unidad(Floor($number/1000))." MIL ".(self::_Centena($number%1000));
		if ($number < 1000) $num = self::_Centena($number);
		return $num;
	}
	private function _DecMiles($number)
	{
		if ($number == 10000) $num = "DIEZ MIL";
		if ($number > 10000 && $number <20000) $num = self::_Decena(Floor($number/1000))."MIL ".(self::_Centena($number%1000));		
		if ($number >= 20000 && $number <100000) $num = self::_Decena(Floor($number/1000))." MIL ".(self::_Miles($number%1000));
		if ($number < 10000) $num = self::_Miles($number);
		return $num;
	}
	private function _CienMiles($number)
	{
		if ($number == 100000) $num = "CIEN MIL";
		if ($number >= 100000 && $number <1000000) $num = self::_Centena(Floor($number/1000))." MIL ".(self::_Centena($number%1000));
		if ($number < 100000) $num = self::_DecMiles($number);
		return $num;
	}
	private function _Millon($number)
	{
		if ($number >= 1000000 && $number <2000000) $num = "UN MILLON ".(self::_CienMiles($number%1000000));
		if ($number >= 2000000 && $number <10000000) $num = self::_Unidad(Floor($number/1000000))." MILLONES ".(self::_CienMiles($number%1000000));
		if ($number < 1000000) $num = self::_CienMiles($number);
		return $num;
	}
	private function _DecMillon($number)
	{
		if ($number == 10000000) $num = "DIEZ MILLONES";
		if ($number > 10000000 && $number <20000000) $num = self::_Decena(Floor($number/1000000))."MILLONES ".(self::_CienMiles($number%1000000));		
		if ($number >= 20000000 && $number <100000000) $num = self::_Decena(Floor($number/1000000))." MILLONES ".(self::_Millon($number%1000000));
		if ($number < 10000000) $num = self::_Millon($number);
		return $num;
	}
	private function _CienMillon($number)
	{
		if ($number == 100000000) $num = "CIEN MILLONES";
		if ($number >= 100000000 && $number <1000000000) $num = self::_Centena(Floor($number/1000000))." MILLONES ".(self::_Millon($number%1000000));		
		if ($number < 100000000) $num = self::_DecMillon($number);
		return $num;
	}
	private function _MilMillon($number)
	{
		if ($number >= 1000000000 && $number <2000000000) $num = "MIL ".(self::_CienMillon($number%1000000000));
		if ($number >= 2000000000 && $number <10000000000) $num = self::_Unidad(Floor($number/1000000000))." MIL ".(self::_CienMillon($number%1000000000));
		if ($number < 1000000000) $num = self::_CienMillon($number);
		return $num;
	}
	
	/*
	 * Enviar MSG a un celular usando BASH/Gammu
	 * crear script llamado owCellSend que ejecute ' printf "$2" | gammu --sendsms TEXT $1 '
	 * dar permisos con /etc/sudoers %nobody  ALL=NOPASSWD: /<ruta_a_script>/owCellSend
	 */ 
	public static function CellSend($numero, $mensaje){
		$mensaje = "\"".$mensaje."\"";
		$execute = 'sudo owCellSend '.$numero. ' ' .$mensaje. ' ';	
		exec($execute, $output);
		if(ereg("OK",$output)){
			return true;
		}
		else if(ereg("OK",$output)){
			return false;
		}
	}	
}

?>